package DMSXL._2024.BinaryTrees;



import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class inorderTraversal {
    public static void main(String[] args) {
        Integer[] nums = {1,2,3,4,5,6};

        TreeNode root = CreateTree.createBinaryTree(nums,0);
        System.out.println(new inorderTraversal().inorderTraversal(root));
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root == null){
            return result;
        }
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            result.add(top.val);
            //
            cur = top.right;
        }

        return result;
    }
}
